﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class DemoStoreProcedure_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "";
        if (!IsPostBack)
        {
            LoadOrigin();
        }
    }

    private void LoadOrigin()
    {
        SqlCommand cmd = DataAccess.Connection.CreateCommand();
        cmd.CommandText = "GetOrigin";
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataReader dr = cmd.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        SqlCommand cmd = DataAccess.Connection.CreateCommand();
        cmd.CommandText = "SearchOriginByName";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@name", TextBox1.Text);
        SqlDataReader dr = cmd.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            SqlCommand cmd = DataAccess.Connection.CreateCommand();
            cmd.CommandText = "InsertOrigin";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Name", TextBox2.Text);
            cmd.Parameters.AddWithValue("@lang", DropDownList1.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@active", (CheckBox1.Checked ? 1 : 0));
            cmd.ExecuteNonQuery();
            LoadOrigin();
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        try
        {
            SqlCommand cmd = DataAccess.Connection.CreateCommand();
            cmd.CommandText = "UpdateOrigin";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id", lblid.Text);
            cmd.Parameters.AddWithValue("@Name", TextBox2.Text);
            cmd.Parameters.AddWithValue("@lang", DropDownList1.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@Active", (CheckBox1.Checked ? 1 : 0));
            cmd.ExecuteNonQuery();
            LoadOrigin();
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        lblid.Text = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
        SqlCommand cmd = DataAccess.Connection.CreateCommand();
        cmd.CommandText = "select * from Origin where originid=@id";
        cmd.Parameters.AddWithValue("@id", lblid.Text);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            if (dr.Read())
            {
                TextBox2.Text = dr.GetString(1);
                DropDownList1.SelectedValue = dr.GetString(2);
                CheckBox1.Checked=(dr.GetInt32(3)==1);
            }
        }
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = DataAccess.Connection.CreateCommand();
        cmd.CommandText = "DeleteOrigin";
        cmd.Parameters.AddWithValue("@id", lblid.Text);
        cmd.ExecuteNonQuery();
        LoadOrigin();
    }
}
